package fun.rockchen.dao;

import fun.rockchen.bean.Tag;
import fun.rockchen.bean.UserMapTag;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author Rock Chen
 * @Description:
 * @date 2023/10/16 10:20
 */

@Mapper
public interface TagDao {
    @Select("""
            select tag.id AS id, tag.tagContent AS tagContent, COUNT(tMT.taskId) AS total
            from tag
                left join taskMapTag tMT on tag.id = tMT.tagId
                left join userMapTag uMT on tag.id = uMT.tagId
            where userId = '${id}'
            GROUP BY tag.id, tag.tagContent;""")
    List<Tag> getTagList(Tag tag);

    @Insert("insert into tag (id, tagContent) values ('${tagId}', '${tagContent}')")
    boolean addTagStep1(UserMapTag userMapTag);

    @Insert("insert into userMapTag (userId, tagId) values ('${userId}', '${tagId}')")
    boolean addTagStep2(UserMapTag userMapTag);

    @Delete("delete userMapTag, tag " +
            "from userMapTag " +
            "left join tag on userMapTag.tagId = tag.id " +
            "where tag.id = '${tagId}' or userMapTag.tagId = '${tagId}'")
    boolean deleteTag(String tagId);
}
